<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/29/029
 * Time: 0:00
 */

namespace app\api\controller;

use think\Hook;
use think\Request;
use think\Loader;
use think\Db;
use think\Session;

class User extends Rest
{

    public function index()
    {

    }

    public function login()
    {
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
//        $validate = Loader::validate('login');
//        if ($validate->scene('login')->check($data)) {
        $studentId = $data['studentId'];
        $password = $data['password'];
        $loginTime = date("Y-m-d H:i:s", time());
        $queryStudent = Db::name('userinfo')->where(['StudentId' => $studentId])->find();
        if ($queryStudent) {
            if ($queryStudent['Type'] == 2) {
                $passwordMd5 = md5(md5($password . "wyjsjxh"));
                $loginResult = Db::name('userlogin')->where('studentId', '=', $studentId)->find();
                if ($loginResult) {
                    if ($loginResult['password'] === $passwordMd5) {
                        $updateResult = Db::name('userlogin')->where(['studentId' => $studentId])->update(['loginTime' => $loginTime]);
                        if ($updateResult) {
                            Session::set("user", $studentId);
                            $this->data['code'] = 1;
                            $this->data['msg'] = '登录成功';
                        } else {
                            $this->data['code'] = -2;
                            $this->data['msg'] = '登录操作过于频繁，请等2分钟再登录';
                        }
                    } else {
                        $this->data['code'] = 0;
                        $this->data['msg'] = '密码错误';
                    }
                } else {
                    $this->data['code'] = -1;
                    $this->data['msg'] = '该账号暂时不能登录请联系管理员';
                }
            } else if ($queryStudent['Type'] == 0) {
                $this->data['code'] = -200;
                $this->data['msg'] = '您被拉入协会黑名单中';
            } else {
                $this->data['code'] = -200;
                $this->data['msg'] = '您暂未通过协会审核';
            }
        } else {
            $this->data['code'] = -200;
            $this->data['msg'] = '您不是协会会员';
        }
        return $this->data;
    }

    public function logOut()
    {
        Session::clear();
        $this->redirect('index');
    }

    public function changePassword()
    {

    }

    public function addAdmin()
    {
        if (1 != self::check_power(3)) {
            return $this->data;
        }
    }

    public function getAdmin()
    {

    }

    public function delAdmin()
    {

    }

    public function editAdmin()
    {

    }

    public function addleave()
    {
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
        $studentId = Session::get("user");
        if (!empty($studentId)) {
            $class = $data["class"];
            $leavefinishdate = $data['leavefinishdate'];
//            $finishdate = $data['finishdate'];
            $reason = $data['reason'];
            $username = Db::name("userinfo")->where(["StudentID" => $studentId])->value("Name");
            if($class == "0"){
                $class = "上课";
            }else{
                $class = "会议";
            }
            $leavedate = "";
            $finishdate = "";
            $explode_date = explode(" 到 ",$leavefinishdate);
            $datas = [
                "studentid"=>$studentId,
                "name" => $username,
                "class"=>$class,
                "leavedate" =>$explode_date[0],
                "finishdate" =>$explode_date[1],
                "reason" => $reason,
                "data_state" =>0,
                "create_date" =>date("Y-m-d H:i:s", time())
            ];
            $insertgetid =  Db::name("leave")->insertGetId($datas);
            if($insertgetid){
                $this->data['code'] = 1;
                $this->data['msg'] = "提交成功";
            }else{
                $this->data['code'] = 0;
                $this->data['msg'] = "提交失败";
           }
        } else {
            $this->data['code'] = -2;
            $this->data['msg'] = "请确定登陆账号再进行请假操作！";
        }

        return $this->data;
    }
    public function getleaves(){
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $studentId = Session::get("user");
        $result =  Db::name("leave")->where(["studentid"=>$studentId])->select();
        foreach($result as $key=>$value){
            if(empty($result[$key]['check_name'])){
                $result[$key]['check_name'] = "暂无";
            }
            if(empty($result[$key]['check_date'])){
                $result[$key]['check_date'] = "暂无";
            }
            if($result[$key]['data_state'] == 0){
                $result[$key]['data_state'] = "未审核";
            }else{
                $result[$key]['data_state'] = "审核通过";
            }
            $result[$key]['leavedate'] = $result[$key]['leavedate']."到".$result[$key]['finishdate'];
        }
        $this->data['code'] = 0;
        $this->data['msg'] = "成功";
        $this->data['data'] = $result;
        return $this->data;
    }
}